﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Prod;
using ESOL.Common.Entity;

namespace ESOL.WinUI.Proj
{
    public partial class frmPrintFormProductionReport : BaseForm.frmBaseDialogyForm
    {
        public frmPrintFormProductionReport()
        {
            InitializeComponent();
        }

        private string GetCondition()
        {
            string Condition = "";
            if (dtpDeliveryDate.EditValue!=null)
            {
                Condition += " AND DELIVERY_DATE>='" + ((DateTime)dtpDeliveryDate.EditValue).ToString("yyyy-MM-dd") + " 00:00:00' ";
            }
            if (dtpDeliveryDateTo.EditValue!=null)
            {
                Condition += " AND DELIVERY_DATE<='" + ((DateTime)dtpDeliveryDateTo.EditValue).ToString("yyyy-MM-dd") + " 23:59:59' ";
            }
            if (string.IsNullOrEmpty(txtPlanbOrderItemCode.Text) == false)
            {
                Condition += " AND ORDER_ITEM_CODE LIKE '%" + txtPlanbOrderItemCode.Text + "%' ";
            }
            if (string.IsNullOrEmpty(txtProdPlanOrderCode.Text) == false)
            {
                Condition += " AND PLAN_ORDER_CODE LIKE '%" + txtProdPlanOrderCode.Text + "%' ";
            }
            if (string.IsNullOrEmpty(txtContractModel.Text) == false)
            {
                Condition += " AND CONTRACT_MODEL LIKE '%" + txtContractModel.Text + "%' ";
            }
            return Condition;
        }

        private DataSet GetBindData()
        {
            DataSet ds = new DataSet();
            string Condition = GetCondition();
            DataTable dtMain = ProdProductTechnicBs.GetDtWorkingGroupList(Condition, "100").Copy();
            dtMain.Columns.Add("ID");
            foreach (DataRow dr in dtMain.Rows)
            {
                dr["ID"] = Guid.NewGuid().ToString();
            }
            dtMain.TableName = "dtMain";
            dtMain.PrimaryKey = new DataColumn[] { dtMain.Columns["ID"] };
            ds.Tables.Add(dtMain);
            if (dtMain.Rows.Count > 0)
            {
                DataTable dtDetail = new DataTable();
                dtDetail.TableName = "dtDetail";
                foreach (DataRow dr in dtMain.Rows)
                {
                    ProdPlanOrderItemEntity mSelectedPlanOrderItem = ProdPlanOrderItemBs.GetProdPlanOrderItemByID(dr["PLAN_ORDER_ITEM_ID"].ToString());
                    DataTable dt = ProdPlanOrderItemBs.GetAssembleTaskList(mSelectedPlanOrderItem.PlanOrderItemId).Copy();

                    DataColumn dc = new DataColumn("ID");
                    dc.DefaultValue = dr["ID"].ToString();
                    dt.Columns.Add(dc);
                    dtDetail.Merge(dt, true);
                }
                ds.Tables.Add(dtDetail);

                DataColumn parentColumns;
                DataColumn childColumns;
                parentColumns = ds.Tables["dtMain"].Columns["ID"];
                childColumns = ds.Tables["dtDetail"].Columns["ID"];
                DataRelation dsdr1 = new DataRelation("fk_1", parentColumns, childColumns);
                ds.Relations.Add(dsdr1);
            }
            return ds;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            DataSet ds = GetBindData();
            frmPrintTemplateProductionReport fXtraReport = new frmPrintTemplateProductionReport(ds);
            printControl1.PrintingSystem = fXtraReport.PrintingSystem;
            fXtraReport.CreateDocument();
        }
    }
}
